package com.erp.staobaotrasg.utils.m;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;


import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Function : xutils 的数据的工具类
 * Created by cyx
 * E-mial: caoyunxiang@youzhu.com
 * on 2016/3/16.
 */
public class MCxUtils {

    private MCxUtils() {
    }

    /**
     * 将map 数据格式 转换为 xutils 使用的 RequestParams 类型
     *
     * @param path
     * @param hparams
     * @return
     */
    public static RequestParams getxUtilsRequestParams(String path, HashMap<String, String> hparams) {
        RequestParams params1 = new RequestParams(path);
        if (hparams != null && !hparams.isEmpty()) {//判断 提交内容 不为空
            for (Map.Entry<String, String> entry : hparams.entrySet()) {//将 集合转换为提交格式
                try {
                    params1.addBodyParameter(entry.getKey(), URLEncoder.encode(entry.getValue(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        return params1;
    }

    /**
     * 获取 字符数据类型的 数据 传出的格式为 String 格式
     *
     * @param path     地址
     * @param hparams  参数
     * @param callback 回调
     */
    public static void getStringDataByPost(final String path, final HashMap<String, String> hparams, final MCHttpCallBackString callback) {
        RequestParams requestParams = getxUtilsRequestParams(path, hparams);

        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                callback.response(s);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                callback.complete();

            }
        });
    }

    /**
     * 获取 字符数据类型的 数据 传出的格式为 String 格式
     *
     * @param path     地址
     * @param hparams  参数
     * @param callback 回调
     */
    public static void getStringDataByPostForPull(final String path, final HashMap<String, String> hparams, final MCHttpCallBackString callback, boolean delay) {
        RequestParams requestParams = getxUtilsRequestParams(path, hparams);
        requestParams.setConnectTimeout(5000);
        requestParams.setMaxRetryCount(0);

        x.http().post(requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String s) {
                callback.response(s);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                callback.complete();
                //ToastUtils.showShort("数据请求错误，请检查网络");

            }

            @Override
            public void onCancelled(CancelledException e) {
                callback.complete();
            }

            @Override
            public void onFinished() {
                callback.complete();

            }
        });
    }


    /**
     * 获取 字符数据类型的 数据 传出的格式为 byte 的数组
     *
     * @param path
     * @param callback
     */
    public static void getStringDataByGET(final String path, final MCHttpCallBackString callback) {
        RequestParams params1 = new RequestParams(path);
        x.http().get(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                callback.response(s);

            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                callback.complete();
            }
        });

    }

    /**
     * xutils 加载图片的 工具 加载之后并且保存
     *
     * @param imgV    要加载地址的图片
     * @param url     地址
     * @param options 传入的参数
     */

    public static void xInitAndSaveImage(ImageView imgV, final String url, ImageOptions options) {
        x.image().bind(imgV, url, options, new Callback.CommonCallback<Drawable>() {
            @Override
            public void onSuccess(Drawable drawable) {
                BitmapDrawable drawable1 = (BitmapDrawable) drawable;
                Bitmap bitmap = drawable1.getBitmap();
                try {
                    MCFileUtil.saveImage(url, bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
            }

            @Override
            public void onCancelled(CancelledException e) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


}
